const tree = {
  val: "a",
  children: [
    {
      val: "c",
      children: [
        {
          val: "e",
          children: [],
        },
      ],
    },
    {
      val: "d",
      children: [
        {
          val: "b",
          children: [],
        },
      ],
    },
  ],
};
console.log("===== 深度优先遍历输出");
const dfs = (root) => {
  console.log(root.val);
  if (root.children) {
    root.children.forEach(dfs);
  }
};

dfs(tree);
console.log("===== 广度优先遍历输出");
const bfs = (root) => {
  const q = [root];
  while (q.length > 0) {
    const n = q.shift();
    console.log(n.val);
    n.children.forEach((child) => {
      q.push(child);
    });
  }
};
bfs(tree);
